package cs294s.liveradar;

import java.util.List;

import android.os.Bundle;
import android.util.Log;



import android.content.res.Configuration;
import android.database.DataSetObserver;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import cs294s.liveradar.data.Blip;
import cs294s.liveradar.service.Feed;
import cs294s.liveradar.service.FeedManager;
import cs294s.liveradar.service.Util;
import cs294s.liveradar.ui.BlipOverlay;
public class RadarMap extends MapActivity{

	private MapView mapView;

	List<Overlay> mapOverlays;

	Drawable drawable;
	BlipOverlay itemizedOverlay;


	private MyDataSetObserver mObserver = new MyDataSetObserver();

	/**
	 * Maintains the state of our data
	 */
	private FeedManager mBlipManager;

	private boolean registeredObserver = false;

	/**
	 * Used by the {@link ImageManager} to report changes in the list back to
	 * this adapter.
	 */
	private class MyDataSetObserver extends DataSetObserver {
		@Override
		public void onChanged() {
			Log.d("RADAR", "OBSERVER RECIEVED ON CHANGED");
			redrawBlips();
		}

		@Override
		public void onInvalidated() {

		}
	}

	public void redrawBlips() {
		for(int i = 0; i < mBlipManager.size(); i++) {
			Feed feed = mBlipManager.get(i);
			for(int j = 0; j < feed.size(); j++) {
				Blip b = feed.get(j);

				Log.d("RADAR", "REDRAWING BLIP: " + b.getLocation().toString());
				OverlayItem overlayItem = new OverlayItem(b.getLocation(),b.getDescription(), Util.timeFormat(b.getEventTime()) + "\nTags: " + b.tagsToString());
				itemizedOverlay.addOverlay(overlayItem);
			}

			mapOverlays.add(itemizedOverlay);
			mapView.invalidate();
		}

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		//ignore orientation change
		super.onConfigurationChanged(newConfig);
	}


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);

		mBlipManager = FeedManager.getInstance(this);

		if(!registeredObserver) {
			Log.d("RADAR", "ADD OBSERVER: " + mObserver.toString());
			mBlipManager.addObserver(mObserver);
			registeredObserver = true;
		}


		//Setting up mapview
		mapView = (MapView)findViewById(R.id.mapview);

		mapOverlays = mapView.getOverlays();
		mapView.setBuiltInZoomControls(true);

		// first overlay
		drawable = getResources().getDrawable(R.drawable.marker2);
		itemizedOverlay = new BlipOverlay(drawable, mapView);


		mBlipManager.updateFeeds();


		int lat = getIntent().getIntExtra("lat", 0);
		int lon = getIntent().getIntExtra("lon", 0);

		if(lat != 0 && lon != 0) {
			GeoPoint focusPoint = new GeoPoint(lat, lon);
			final MapController mc = mapView.getController();
			mc.animateTo(focusPoint);
			mc.setZoom(17); 
		}

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d("RADAR", "RADAR MAP ONSTOP");
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		mBlipManager.removeObserver(mObserver);
		Log.d("RADAR", "RADAR MAP ONDESTROY");
	}

}
